<template>
	<BaseContainer>
		<aplid-base-crud ref="crud" class="w-100" v-bind="baseConfig">
			<template v-slot:appName="{ row }">
				<span v-if="row.ssoType == 0">{{ row.appName }}</span>
				<el-button v-else text type="primary" @click="handleLinkto(row)">{{ row.appName }}</el-button>
			</template>
			<template v-slot:menuColumn="{ row }">
				<el-button text type="primary" @click="handleTestEncrypt(row)">测试</el-button>
			</template>
			<template v-slot:detail-btn="{ row }">
				<el-button text type="primary" @click="handleRefreshConfig(row)">重新生成凭证</el-button>
			</template>
		</aplid-base-crud>

		<el-drawer v-model="drawer" title="测试">
			<el-input v-model="testEncryptStr" :autosize="{ minRows: 6, maxRows: 8 }" type="textarea" placeholder="请输入需要处理的数据" />
			<div class="result">
				{{ resultStr }}
			</div>
			<template v-slot:footer>
				<div class="flex">
					<el-button class="flex1" @click="handleVerifyg(1)">验签</el-button>
					<el-button class="flex1" type="primary" @click="onTestEncrypt(2)">加解密</el-button>
				</div>
			</template>
		</el-drawer>
	</BaseContainer>
</template>

<script setup>
import { baseConfig } from './config'
import apiMod from '@/apiMod'

const crud = ref(null)
const drawer = ref(false)
const testEncryptStr = ref()
const resultStr = ref()
const nowRowData = ref({})
const handleRefreshConfig = (row) => {
	apiMod.view_sysSsoConfig_refreshSysSsoConfig({ id: row.id }).then(() => {
		ElMessage({
			message: '刷新成功',
			type: 'success',
		})
		handleRefresh(true)
	})
}

const handleVerifyg = () => {
	apiMod.view_sysSsoConfig_verifySignHex({ id: nowRowData.value.id, sign: testEncryptStr.value }).then((res) => {
		resultStr.value = res.data
		ElMessage({
			message: '操作成功',
			type: 'success',
		})
	})
}

const handleLinkto = (row) => {
	apiMod.view_sso_ssoRelease(row.id).then((res) => {
		if (!res.data.includes('error')) {
			window.open(res.data, '_blank')
		} else {
			ElMessage({
				message: 'SSO 认证失败!',
				type: 'error',
			})
		}
	})
}

const handleTestEncrypt = (row) => {
	testEncryptStr.value = ''
	resultStr.value = ''
	nowRowData.value = row
	drawer.value = true
}

const onTestEncrypt = () => {
	apiMod
		.view_sysSsoConfig_testEncrypt({
			appId: nowRowData.value.id,
			content: testEncryptStr.value,
		})
		.then((res) => {
			resultStr.value = res.data
			ElMessage({
				message: '操作成功',
				type: 'success',
			})
		})
}

const handleRefresh = (visible = false) => {
	nextTick(() => {
		crud.value?.refreshChange({}, visible)
	})
}
</script>

<style lang="scss" scoped></style>
